/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-10-7
 * @Time  : 下午3:57
 * @brief :
 */
(function () {
    var util = require("wap_common:widget/ui/libs/util.js").util,
        $page = $(".activity-register"),
        $tip = $page.find(".tip"),
        $username = $page.find(".phone"),
        $code = $page.find(".check-code"),
        $password = $page.find(".password"),
        $protocol = $("#protocol"),
        $openId = $page.find(".open-id"),
        $register_from = $page.find(".register-from"),
        $phoneBtn = $page.find(".btn-phone"),
        $registerBtn = $page.find(".btn-register");

    function tipShow(text){
        $tip.css("visibility","visible").text(text);
    }

    function tipHide(){
        $tip.css("visibility","hidden").text("");
    }

    //前段验证
    function check(){
        tipHide();
        if(!(/^\d{6}$/.test($code.val()))){
            tipShow("验证码输入错误");
            return false;
        }else if(!util.checkPassword($password.val())){
            tipShow("密码不是6-16位字符");
            return false;
        }if(!$("#protocol:checked").length){
            tipShow("请确认同意真融宝协议");
            return false;
        }else{
            return true;
        }
    }

    function getCodeTime(){
        var count = 60,
            countInterval = setInterval(function(){
                $phoneBtn.text(count);
                if(count>0){
                    count--;
                }else{
                    count = 60;
                    clearInterval(countInterval);
                    $phoneBtn.removeClass("btn-phone-disable")
                        .text("未收到,语音获取").attr("data-type",1);
                }
            },1000);
    }

    $phoneBtn.on("click",function(){
        var me = this;
        if(!($(this).hasClass("btn-phone-disable"))){
            var username = $.trim($username.val());
            tipHide();
            $phoneBtn.addClass("btn-phone-disable").text("正在获取");
            $.post("/account/sendidentitycodewap",{
                mobile:username,
                type:$(me).attr("data-type")
            },function(ret,status){
                if(status == "success"){
                    if(ret.error_no == 0){
                        getCodeTime();
                    }else{
                        if(ret.error_no == 1){
                            //如果是因为一分钟获取验证码过多
                            setTimeout(function(){
                                $phoneBtn.removeClass("btn-phone-disable").text("重新获取");
                            },60*1000);
                            $phoneBtn.text("一分钟后重试");
                        }
                        tipShow(ret.error_message);
                    }
                }else{
                    tipShow("网络错误,请重试");
                    $phoneBtn.removeClass("btn-phone-disable").text("重复获取");
                }
            },"json")
        }
    }).trigger("click");


    $registerBtn.on("click",function(){
        if( !($(this).hasClass("btn-disable"))){
            if(check()){
                $(this).addClass("btn-disable");
                $.post("/account/registeruser",{
                    user_name:     $.trim($username.val()),
                    code:          $.trim($code.val()),
                    passwd:        $.trim($password.val()),
                    register_from: $.trim($register_from.val())
//                    open_id:     $openId.val()
                },function(ret,status){
                    if(status == "success"){
                        if( ret.error_no == 0){
                             location.href="/wap/index?from_register=true"
                        }else{
                            tipShow(ret.error_message);
                        }
                    }else{
                        tipShow("网络错误,请重试");
                    }
                    $phoneBtn.removeClass("btn-phone-disable");
                    $registerBtn.removeClass("btn-disable");
                },"json")
            }
        }
    });

})();